//
//  AppDelegate.swift
//  微博项目
//
//  Created by admin on 16/3/1.
//  Copyright © 2016年 jin. All rights reserved.
//
import AVFoundation
import UIKit
let versionKey = "versionKey"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // print(accountFilePath)
        
        // 为了提高运行级别，伪装成播放音乐的app
        // 设置音频会话
        let session = AVAudioSession.sharedInstance()
        
        // 后台播放
        let _ = try? session.setCategory(AVAudioSessionCategoryPlayback)
        // 单独播放一个后台程序
        let _ = try? session.setCategory(AVAudioSessionCategorySoloAmbient)
        
        // [session setActive:YES error:nil];
        let _ = try? session.setActive(true)
        
        // 获取通知权限，ios8 以下的不知道是什么样的。。。。先不管了
        if #available(iOS 8.0, *) {
            let applicationSetting = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil)
            application.registerUserNotificationSettings(applicationSetting)
        } else {
            // Fallback on earlier versions
//            let myTypes = UIRemoteNotificationType.Badge
//            application.registerForRemoteNotificationTypes(myTypes)
        }
        
        // 自定义window
        let window = UIWindow()
        window.frame = UIScreen.mainScreen().bounds
        let oauth2 = Oauth2Controller()
        // 设置window根控制器
        self.window = window
        self.window?.makeKeyAndVisible()
        if Account.shareAccount() == nil
        {
            self.window?.rootViewController = oauth2
        }
        else
        {
            ChoiceController.chooseController()
        }
        return true
    }
    var player:AVAudioPlayer?
    // 失去焦点
    func applicationWillResignActive(application: UIApplication) {
        // 为了提高程序的优先级，伪装成了音乐播放程序，所以这里需要播放，不然系统会查出来的。。。。
        let url = NSBundle.mainBundle().URLForResource("music.mp3", withExtension: nil)
        let player = try? AVAudioPlayer(contentsOfURL: url!)
        
        player?.prepareToPlay()
        // 无限播放
        player?.numberOfLoops = -1
        // 播放
        player?.play()
        
        self.player = player;
    }
    // 程序进入后台的时候调用
    //后台任务
    var backgroundTask:UIBackgroundTaskIdentifier! = nil
    func applicationDidEnterBackground(application: UIApplication) {
        // 开启一个后台任务,时间不确定，优先级比较低，假如系统要关闭应用，首先就考虑
        //注册后台任务
//        self.backgroundTask = application.beginBackgroundTaskWithExpirationHandler({
//            () -> Void in
//            //如果没有调用endBackgroundTask，时间耗尽时应用程序将被终止
//            application.endBackgroundTask(self.backgroundTask)
//            self.backgroundTask = UIBackgroundTaskInvalid
//        })
    }

    func applicationWillEnterForeground(application: UIApplication) {
        self.player?.stop()
        self.player = nil
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

